package com.dawn.module.cms.controller.admin.article;

import cn.hutool.core.collection.CollUtil;
import com.dawn.framework.common.util.object.BeanUtils;
import com.dawn.module.cms.controller.admin.article.vo.ArticlePageReqVO;
import com.dawn.module.cms.controller.admin.article.vo.ArticleRespVO;
import com.dawn.module.cms.controller.admin.article.vo.ArticleSaveReqVO;
import com.dawn.module.cms.controller.admin.category.vo.CategoryRespVO;
import com.dawn.module.cms.convert.article.ArticleConvert;
import com.dawn.module.cms.convert.category.CategoryConvert;
import com.dawn.module.cms.dal.dataobject.category.CategoryDO;
import com.dawn.module.cms.service.category.CmsCategoryService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.dawn.framework.common.pojo.PageParam;
import com.dawn.framework.common.pojo.PageResult;
import com.dawn.framework.common.pojo.CommonResult;
import static com.dawn.framework.common.pojo.CommonResult.success;

import com.dawn.framework.excel.core.util.ExcelUtils;


import static com.dawn.framework.common.util.collection.CollectionUtils.convertList;

import com.dawn.module.cms.dal.dataobject.article.CmsArticleDO;
import com.dawn.module.cms.service.article.CmsArticleService;

@Tag(name = "管理后台 - 文章")
@RestController
@RequestMapping("/cms/article")
@Validated
public class CmsArticleController {

    @Resource
    private CmsArticleService articleService;

    @Resource
    private CmsCategoryService categoryService;

    @PostMapping("/create")
    @Operation(summary = "创建文章")
    @PreAuthorize("@ss.hasPermission('cms:article:create')")
    public CommonResult<Long> createArticle(@Valid @RequestBody ArticleSaveReqVO createReqVO) {
        return success(articleService.createArticle(createReqVO));
    }

    @PostMapping("/update")
    @Operation(summary = "更新文章")
    @PreAuthorize("@ss.hasPermission('cms:article:update')")
    public CommonResult<Boolean> updateArticle(@Valid @RequestBody ArticleSaveReqVO updateReqVO) {
        articleService.updateArticle(updateReqVO);
        return success(true);
    }

    @GetMapping("/delete")
    @Operation(summary = "删除文章")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('cms:article:delete')")
    public CommonResult<Boolean> deleteArticle(@RequestParam("id") Long id) {
        articleService.deleteArticle(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得文章")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('cms:article:query')")
    public CommonResult<ArticleRespVO> getArticle(@RequestParam("id") Long id) {
        CmsArticleDO article = articleService.getArticle(id);
        return success(BeanUtils.toBean(article, ArticleRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得文章分页")
    @PreAuthorize("@ss.hasPermission('cms:article:query')")
    public CommonResult<PageResult<ArticleRespVO>> getArticlePage(@Valid ArticlePageReqVO pageReqVO) {
        PageResult<CmsArticleDO> pageResult = articleService.getArticlePage(pageReqVO);
        if (CollUtil.isEmpty(pageResult.getList())) {
            return success(new PageResult<>(pageResult.getTotal()));
        }
        // 拼接数据
        Map<Long, CategoryDO> categoryMap = categoryService.getCategoryMap(
                convertList(pageResult.getList(), CmsArticleDO::getCategoryId));
        return success(new PageResult<>(ArticleConvert.INSTANCE.convertList(pageResult.getList(), categoryMap),
                pageResult.getTotal()));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出文章 Excel")
    @PreAuthorize("@ss.hasPermission('cms:article:export')")
    public void exportArticleExcel(@Valid ArticlePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CmsArticleDO> list = articleService.getArticlePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "文章.xls", "数据", ArticleRespVO.class,
                        BeanUtils.toBean(list, ArticleRespVO.class));
    }

}